import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_video_category.dart';
import 'package:flutter_learn_vi/src/bean/bean_video_course.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/page/study/video_item_page.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/11.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class VideoItemRequestPage extends StatefulWidget {
  ///视频课程分类
  final BeanVideoCategory itemBean;

  //1为党建
  final int pageIndex;
  final int taskId;

  VideoItemRequestPage(
      {Key key, @required this.itemBean, this.pageIndex = 1, this.taskId})
      : super(key: key);

  @override
  _NewsItemRequestPageState createState() => _NewsItemRequestPageState();
}

class _NewsItemRequestPageState extends State<VideoItemRequestPage> {
  List<BeanVideoCourse> _list;

  bool isClickRefresh = false;

  @override
  void initState() {
    super.initState();
    getViceoCourseList();
  }

  @override
  Widget build(BuildContext context) {
    if (_list == null) {
      return LoadingWidget();
    }
    if (_list.length == 0) {
      return NoDataWidget(
        text: "课程内容即将更新，请耐心等待",
        clickCallBack: () {
          isClickRefresh = true;
          getViceoCourseList();
        },
      );
    }
    return VideoItemPage(
      taskId: widget.taskId,
      list: _list,
    );
  }

  CancelToken _cancelToken;

  Future<bool> getViceoCourseList() async {
    Map<String, dynamic> map = Map();
    map["videoCategoryId"] = widget.itemBean.id;
    //post请求发送
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.videoCourseUrl,
      queryParameters: map,
      cancelTag: _cancelToken,
    );

    if (responseInfo.success) {
      _list = [];
      List list = responseInfo.data;
      list.forEach((element) {
        BeanVideoCourse beanVideoCourse = BeanVideoCourse.fromMap(element);
        _list.add(beanVideoCourse);
      });
    }

    if (!mounted) return true;
    if (isClickRefresh && (_list == null || _list.length == 0)) {
      isClickRefresh = false;
      ToastUtils.showToast("已刷新");
    }
    setState(() {});

    return Future.value(true);
  }

  @override
  void dispose() {
    super.dispose();
    if (_cancelToken != null &&
        _cancelToken.isCancelled != null &&
        !_cancelToken.isCancelled) {
      _cancelToken.cancel();
    }
  }
}
